<template>
  <div style="padding: 10px">
    <!--    功能区-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary" @click="insert">导入</el-button>
    </div>
    <!--    搜索区-->
    <div style="margin: 10px 0">
      <el-input v-model="keyword" placeholder="请输入关键字" style="width: 20%" clearable/>
      <el-button type="primary" style="margin-left: 5px" @click="searchTeaInfo">查询</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%" stripe>
      <el-table-column prop="name" label="姓名"/>
      <el-table-column prop="type" label="用户角色"/>
      <el-table-column prop="workNumber" label="工号" sortable/>
      <el-table-column prop="idCard" label="身份证"/>
      <el-table-column prop="phoneNumber" label="手机号码"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="password" label="密码"/>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <div v-if="scope.row.status">
            在职
          </div>
          <div v-else>
            离职
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除此信息？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />

      <el-dialog v-model="dialogVisible" title="提示" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="姓名">
            <el-input v-model="form.name" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="身份证">
            <el-input v-model="form.idCard" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="用户角色" v-model="form.type">
            <el-radio disabled>学生</el-radio>
            <el-radio value="2">教师</el-radio>
          </el-form-item>

          <el-form-item label="工号" v-if="isShow">
            <el-input v-model="form.workNumber" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.phoneNumber" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="学院">
            <el-select
                v-model="form.schoolId"
                placeholder="请选择"
                @change="onSchoolChange">
              <el-option v-for="item in schoolList"
                         :key="item.schoolId"
                         :label="item.schoolName"
                         :value="item.schoolId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业">
            <el-select v-model="form.majorId" placeholder="请选择">
              <el-option
                  v-for="item in majorList"
                  :key="item.id"
                  :label="item.majorName"
                  :value="item.majorId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-radio v-model="form.status" :label="true">在职</el-radio>
            <el-radio v-model="form.status" :label="false">离职</el-radio>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </template>
      </el-dialog>
      <el-dialog v-model="insertBoxVisible" title="导入" width="30%" center>
        <el-upload
            class="upload"
            action="http://124.70.179.188/stu/saveStu"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-error="handleError"
            :on-success="handleSuccess"
            :before-remove="beforeRemove"
            :file-list="fileList"
            :multiple="false"
            :limit="1"
            :on-exceed="handleExceed"
            accept=".csv"
        >
          <el-button slot="trigger" size="small" type="primary" style="margin-left: 110px">选取文件</el-button>
          <div slot="tip" class="el-upload__tip" style="margin-left: 110px">只支持csv文件的上传</div>
        </el-upload>

        <template #footer>
          <span class="dialog-footer">
            <el-button @click="insertBoxVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>

  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name: 'TeaMgt',
  components: {},
  data() {
    return {
      form: {type: '1'},
      fileList: [],
      tableData: [],
      schoolList: [],
      majorList: [],

      dialogVisible: false,
      insertBoxVisible: false,
      keyword: '',
      currentPage: 1,
      pageSize: 10,
      total: 2,
      isShow: false

    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("http://localhost:8082/tea/teaInfo").then(res => {
        this.tableData = res.data
        this.total = res.data.length;
      })

      this.getSchoolList()
    },
    //查询
    searchTeaInfo() {
      request.get("http://localhost:8082/tea/teaInfo?keyword=" + encodeURI(encodeURI(this.keyword))).then(res => {
        this.tableData = res.data
        this.total = res.data.length;
      })
    },
    //导入文件
    insert() {
      this.insertBoxVisible = true
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    handleError(response, file, fileList) {
      this.$message.error('上传失败')
    },
    handleSuccess(response, file, fileList) {
      this.$message.success('上传成功')
      this.load() // 刷新表格数据
    },
    handlePreview(file) {
      console.log(file);
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    confirm() {
      this.insertBoxVisible = false // 关闭弹窗
    },
    //用户信息增删查改
    add() {
      this.isShow = true
      this.dialogVisible = true
      this.form = {}
    },
    save() {
      if (this.form.id) { //若id存在则更新数据（编辑），若id不存在则新增数据
        request.put("http://124.70.179.188:8082/tea/updateTea", this.form).then(res => {
          console.log(res)
          // 判断是否返回成功，若失败给出失败的信息
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.message
            })
          }
          this.load() // 刷新表格数据
          this.dialogVisible = false // 关闭弹窗
        })
      } else {
        request.post("http://124.70.179.188:8082/tea/saveTea", this.form).then(res => {
          console.log(res)
          // 判断是否返回成功，若失败给出失败的信息
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.message
            })
          }
          this.load() // 刷新表格数据
          this.dialogVisible = false // 关闭弹窗
        })
      }
    },
    handleEdit(row) {
      this.isShow = false
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(id) { //

      request.delete("http://124.70.179.188:8082/tea/" + id).then(res => {
        // 判断是否返回成功，若失败给出失败的信息
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.message
          })
        }
        this.load() // 删除后重新加载表格数据
      })
    },
    //表格内学院和专业的信息选择
    getSchoolList() {

      request.get("http://124.70.179.188:8082/dpt/schoolList").then(res => {
        this.schoolList = res.data
      })
    },
    onSchoolChange(schoolId) {
      this.form.majorName = ""
      this.getMajorList(schoolId)
    },
    getMajorList(schoolId) {

      request.get("http://124.70.179.188:8082/dpt/majorList?schoolId=" + schoolId).then(res => {
        if (res.code === 0) {
          this.majorList = res.data
        }
      })

    },
    //翻页
    handleSizeChange(pageSize) { // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) { //改变当前页码触发
      this.currentPage = pageNum
      this.load()
    }
  }
}
</script>
